package service.user;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import service.CommonServlet;

public class EditUser extends CommonServlet {
	
	@Override
	protected void execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try
		{
			String newName = request.getParameter("newName");
			String newPwd = request.getParameter("newPwd");
			String sql = "select * from user where u_no = ?";
			PreparedStatement pstmt = mConnection.prepareStatement(sql);
			pstmt.setInt(1, uNo);
			ResultSet rs = pstmt.executeQuery();
			if(rs.next())
			{
				if(newName==null)
					newName=rs.getString("u_name");
				if(newPwd==null)
					newPwd=rs.getString("u_pwd");
			}
			if(newName==null||newPwd==null)
				throw new Exception("some of new info is empty");
			sql="update user set u_name = ?, u_pwd = ? where u_no = ?";
			pstmt = mConnection.prepareStatement(sql);
			pstmt.setString(1,newName);
			pstmt.setString(2,newPwd);
			pstmt.setInt(3,uNo);
			int result = pstmt.executeUpdate();
			
			response.getWriter().println(result);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * Constructor of the object.
	 */
	public EditUser() {
		super();
	}

}
